package com.vonchange.headb.utils.string;

import java.text.MessageFormat;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.vonchange.headb.utils.convert.HConvertUtils;
import com.vonchange.headb.utils.uuid.UUIDUtil;

/**
 * 字符串组件
 * 
 * @author von_change@163.com
 * @date 2015-6-14 下午10:39:45
 */
public class HStringUtils extends com.vonchange.headb.utils.string.third.StringUtils {
	/**
	 * 是否为英文
	 * 
	 * @param checkValue
	 * @return boolean
	 */
	public static boolean isEnglish(String checkValue) {
		String el = "^[A-Za-z]+$";
		Pattern p = Pattern.compile(el);
		Matcher m = p.matcher(checkValue);
		return m.matches();
	}

	/**
	 * 生成不带_的uuid
	 * 
	 * @return 32位的uuid
	 */
	public static String uuid() {
		return UUIDUtil.gen();
	}

	/**
	 * 简单模板实现
	 * 
	 * @param pattern
	 * @param arguments
	 * @return String
	 */
	public static String format(String pattern, Object... arguments) {
		MessageFormat temp = new MessageFormat(pattern);
		return temp.format(arguments);
	}

	public static String substringBefore(String str, String separator, int num) {
		String[] strs = HStringUtils.split(str, separator);
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < num; i++) {
			sb.append(strs[i]).append(separator);
		}
		return HStringUtils.substringBeforeLast(sb.toString(), separator);
	}

	public static String substringBefore(String[] strs, String separator, int num) {
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < num; i++) {
			sb.append(strs[i]).append(separator);
		}
		return HStringUtils.substringBeforeLast(sb.toString(), separator);
	}

	/**
	 * 拼接字符串
	 * 
	 * @param strs
	 * @return String
	 */
	public static String addStr(String... strs) {
		StringBuffer sb = new StringBuffer();
		for (String string : strs) {
			sb.append(string);
		}
		return sb.toString();
	}
	public static String StrNums(String str, String split, int num) {
		if(num==0){
			return "";
		}
		StringBuffer fullStr = new StringBuffer();
		for (int i = 0; i < num; i++) {
			fullStr.append(str).append(split);
		}
		return fullStr.substring(0, fullStr.length()-split.length());
	}

	public static String StrList(List<String> strs, String split) {
		if(null==strs||strs.size()==0){
			return "";
		}
		StringBuffer fullStr = new StringBuffer();
		for (String string : strs) {
			fullStr.append(string).append(split);
		}
		return fullStr.substring(0, fullStr.length()-split.length());
	}

	/**
	 * 拼接字符串
	 * 
	 * @param objs
	 * @return String
	 */
	public static String addStr(Object... objs) {
		StringBuffer sb = new StringBuffer();
		for (Object obj : objs) {
			sb.append(HConvertUtils.toString(obj));
		}
		return sb.toString();
	}

	public static Boolean isNull(Object obj) {
		if (null == obj) {
			return true;
		}
		String str = String.valueOf(obj);
		if (HStringUtils.isBlank(str)) {
			return true;
		}
		return false;
	}

	public static Boolean isNotNull(Object obj) {
		return !isNull(obj);
	}
}
